<?php 
class SurveyResponse
{
	public $Id;
	public $SurveyId;
	public $Subject;
	public $Valid;
	public $Complete;
	public $QuesCount;
	public $AnswerValuesScale;
	public $DemographyValues;
	
	public function SurveyResponse() 
	{ 
		self::setValid(false); 
		self::setComplete(false); 
	}
	
	public function getId() { return $this->Id; }
	public function getSurveyId() { return $this->SurveyId; }
	public function getSubject() { return $this->Subject; }
	public function getValid() { return $this->Valid; }
	public function getComplete() { return $this->Complete; }
	public function getQuesCount() { return $this->QuesCount; }
	
	public function setId($val) { $this->Id=$val; }
	public function setSurveyId($val) { $this->SurveyId=$val; }
	public function setSubject($val) { $this->Subject=$val; }
	public function setValid($val) { $this->Valid=$val; }
	public function setComplete($val) { $this->Complete=$val; }
	public function setQuesCount($val) { $this->QuesCount=$val; }
	
	public function getTotalAnswersCount()
	{
		$count = 0;
		if($this->AnswerValuesScale)
		{ $count += count($this->AnswerValuesScale); }
		return $count;
	}
	
	public function checkSetValid()
	{
		$this->Valid = false;
		if(($this->SurveyId > 0) && ($this->QuesCount == self::getTotalAnswersCount()))
		{
			$this->Valid = true;
			if($this->AnswerValuesScale)
			{ 
				foreach($this->AnswerValuesScale as $answer)
				{
					if(!($answer->getValid()))
					{ $this->Valid = false; }
				}
			}
		}
		return $this->Valid;
	}
	
	public function insertResponse()
	{
		if(self::checkSetValid())
		{
			if($this->Id > 0)
			{ self::clearDbValues(); }
			else
			{
				$sql = 'INSERT INTO '.DB_PERFIX.'survey_response (`survey_id` ,`subject`, `valid`) VALUES ('.$this->SurveyId.', "'.$this->Subject.'", 1);';
				$result = mysql_query($sql);
				self::loadInsertedId();
			}
			if($this->Id > 0)
			{
				if($this->DemographyValues)
				{ 
					foreach($this->DemographyValues as $demogs)
					{
						$demogs->setResponseId($this->Id);
						$demogs->insertValues();
					}
				}
				
				if($this->AnswerValuesScale)
				{ 
					foreach($this->AnswerValuesScale as $answer)
					{
						$answer->setResponseId($this->Id);
						$answer->insertValues();
					}
					self::loadValues();
				}
			}
		}
	}
	
	private function clearDbValues()
	{
		$sql = 'DELETE FROM '.DB_PERFIX.'survey_answer_values_scale WHERE response_id='.$this->Id.';';
		$result = mysql_query($sql);
	}
		
	public function loadInsertedId()
	{
		$sql = 'SELECT id FROM '.DB_PERFIX.'survey_response WHERE survey_id='.$this->SurveyId.' ORDER BY id DESC LIMIT 1;';
		$result = mysql_query($sql);
		if($result)
		{
			while($row = mysql_fetch_array($result))
			{ self::setId($row['id']); }
		}
	}
	
	public function loadValues()
	{
		if($this->Id > 0)
		{ self::loadScaleValues(); }
	}
	
	private function loadScaleValues()
	{
		$sql = 'SELECT id, question_id, value, value2 FROM '.DB_PERFIX.'survey_answer_values_scale WHERE response_id='.$this->Id.';';
		$result = mysql_query($sql);
		if($result)
		{
			while($row = mysql_fetch_array($result))
			{ 
				$this->AnswerValuesScale[$row['question_id']] = new AnswerValueScale();
				$this->AnswerValuesScale[$row['question_id']]->setId($row['id']);
				$this->AnswerValuesScale[$row['question_id']]->setResponseId($this->Id);
				$this->AnswerValuesScale[$row['question_id']]->setQuestionId($row['question_id']);
				$this->AnswerValuesScale[$row['question_id']]->setValue($row['value']);
				$this->AnswerValuesScale[$row['question_id']]->setValue2($row['value2']);
			}
		}
	}
}

class ResponseDemographyValue
{
	public $Id;
	public $SurveyId;
	public $ResponseId;
	public $Value;
	
	public function ResponseDemographyValue() 
	{ self::setValid(false); }
	
	public function getId() { return $this->Id; }
	public function getSurveyId() { return $this->SurveyId; }
	public function getResponseId() { return $this->ResponseId; }
	public function getValid() { return $this->Valid; }
	public function getValue() { return $this->Value; }
	
	public function setId($val) { $this->Id=$val; }
	public function setSurveyId($val) { $this->SurveyId=$val; }
	public function setResponseId($val) { $this->ResponseId=$val; }
	public function setValid($val) { $this->Valid=$val; }
	public function setValue($val) { $this->Value=$val; }
	
	public function insertValues()
	{
		if(($this->SurveyId > 0) && ($this->ResponseId > 0) && ($this->Value > 0))
		{
			$sql = 'INSERT INTO '.DB_PERFIX.'survey_response_demog_vals (';
			$sql .= ' `survey_id` , `response_id` ';
			if($this->Value > 0)
			{ $sql .= ' ,`value`'; }
			$sql .= ') VALUES (';
			$sql .= $this->SurveyId.' ,'.$this->ResponseId;
			if($this->Value > 0)
			{ $sql .= ' ,'.$this->Value; }
			$sql .= ');';
			$result = mysql_query($sql);
		}
	}
}

class AnswerValue
{
	public $Id;
	public $ResponseId;
	public $QuestionId;
	public $Valid;
	
	public function AnswerValue() 
	{ self::setValid(false); }
	
	public function getId() { return $this->Id; }
	public function getResponseId() { return $this->ResponseId; }
	public function getQuestionId() { return $this->QuestionId; }
	public function getValid() { return $this->Valid; }
	
	public function setId($val) { $this->Id=$val; }
	public function setResponseId($val) { $this->ResponseId=$val; }
	public function setQuestionId($val) { $this->QuestionId=$val; }
	public function setValid($val) { $this->Valid=$val; }
}

class AnswerValueScale extends AnswerValue
{
	public $Value;
	public $Value2;
	
	public function getValue() { return $this->Value; }
	public function getValue2() { return $this->Value2; }
	
	public function setValue($val) { $this->Value=$val; }
	public function setValue2($val) { $this->Value2=$val; }
	
	public function insertValues()
	{
		if(($this->Valid) && ($this->ResponseId > 0) && ($this->QuestionId > 0))
		{
			$sql = 'INSERT INTO '.DB_PERFIX.'survey_answer_values_scale (';
			$sql .= ' `response_id` ,`question_id`';
			if($this->Value > 0)
			{ $sql .= ' ,`value`'; }
			if($this->Value2 > 0)
			{ $sql .= ' ,`value2`'; }
			$sql .= ') VALUES (';
			$sql .= $this->ResponseId.' ,'.$this->QuestionId;
			if($this->Value > 0)
			{ $sql .= ' ,'.$this->Value; }
			if($this->Value2 > 0)
			{ $sql .= ' ,'.$this->Value2; }
			$sql .= ');';
			$result = mysql_query($sql);
		}
	}
}

?>